<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="字典项名称">
              <a-input v-model="queryParam.name" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="类型编码">
              <a-input v-model="queryParam.typeCode" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="类型名称">
                <a-input v-model="queryParam.typeName" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="是否已删除">
                <a-select v-model="queryParam.state" :allowClear="true">
                  <a-select-option value="-1">是</a-select-option>
                  <a-select-option value="1">否</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="openAdd">添加</a-button>
              <a-button type="primary" @click="batchDelete">删除</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <dictionary-edit
      ref="dictionaryEdit"
      :visible.sync="visible"
      :editId ="editId"
      @cancel="visible = false"
      @create="handleCreate"
    />
   

    <dictionary-detail
      ref="dictionaryDetail"
      :detailInfo="detailInfo"
    />
    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection">

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

      <span slot="status" slot-scope="text">
        {{ text === 1 ? '否' : '是' }}
      </span>
       
       <template slot="operation" slot-scope="text,record">
        <a @click="openEdit(record)">查询详细</a>
         <a-divider type="vertical" />
        <a @click="openAdit(record)">修改</a>
      </template>
    </s-table>

  </a-card>
</template>

<script>
import { STable } from '~'
import dictionaryEdit from './dictionaryEdit' //新增
import dictionaryDetail from './dictionaryDetail'

export default {
  name: 'TableList',
  components: {
    STable,
    dictionaryEdit,
    dictionaryDetail
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      editId:'',
      detailInfo: {},
      // 查询参数
      queryParam: {},
      postParams: {
        sssfbz: true
      },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '字典项编码',
          dataIndex: 'code'
        },
        {
          title: '字典项名称',
          dataIndex: 'name'
        },
        {
          title: '类型编码',
          dataIndex: 'typeCode'
        },
        {
          title: '类型名称',
          dataIndex: 'typeName'
        },
        {
          title: '排序',
          dataIndex: 'order'
        },
        {
          title: '是否已删除',
          dataIndex: 'state',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.base.dictionaryList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false
    }
  },
  created () {
  },
  methods: {
    handleCreate () {
      const form = this.$refs.dictionaryEdit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        let baseApi = this.editId ? 'dictionaryUpdate' : 'dictionaryAdd'
        this.$api.base[baseApi](Object.assign(values,{id:this.editId})).then((res) => {
          this.$message.success(res.msg)
          this.$refs.table.refresh(true)
        })
        form.resetFields()
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的数据字典')
        return
      }
      this.$confirm({
        title: '确定删除所选中的数据字典?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let dictionaryIds = []
          this.selectedRows.forEach(dictionary => {
            dictionaryIds.push(dictionary.id)
          })
          this.$api.base.dictionaryDelete(dictionaryIds.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
      console.log(this.selectedRowKeys)
      console.log(this.selectedRows)
    },
    openAdd () {
      this.editId = null
      this.visible = true
      this.$refs.dictionaryEdit.form.resetFields()
    },
    openEdit (dictionary) {
      // this.$api.base.dictionaryDetail(dictionary.id)
      this.$refs.dictionaryDetail.visible = true
      this.detailInfo = dictionary
    },
    openAdit(dictionary){
      this.visible = true;
      this.editId = dictionary.id
      this.$nextTick(()=>{
      this.$refs.dictionaryEdit.form.setFieldsValue(dictionary)
      
      })
    },
    handleAditCreate(){

    }

  }
}
</script>
